<template>
  <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm from"
  >
    <el-form-item label="账号" prop="mobileNumber">
      <el-input
          v-model.number="ruleForm.mobileNumber"
          placeholder="请输入手机号码"
          autocomplete="off"
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
          type="password"
          placeholder="请输入密码"
          v-model="ruleForm.password"
          @keyup.enter.native="submitForm('ruleForm')"
      />
    </el-form-item>
    <el-form-item>
      <el-col :span="8">
        <el-button type="primary" @click="submitForm('ruleForm')"
        >登录
        </el-button
        >
      </el-col>
      <el-col :span="8" style="text-align: center">
        <el-button @click="$router.push('/sign-up')">注册</el-button>
      </el-col>
      <el-col :span="8">
        <el-button style="float: right" @click="resetForm('ruleForm')"
        >重置
        </el-button
        >
      </el-col>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "SignIn",
  data() {
    return {
      ruleForm: {
        mobileNumber: "",
        password: "",
      },
      rules: {
        mobileNumber: [
          {
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请输入账号"));
              } else {
                if (!Number.isInteger(value)) {
                  callback(new Error("账号必须为数字值"));
                } else {
                  value = Number.parseInt(value);
                  if (value < 0 || value > 20000000000) {
                    callback(new Error("账号范围有误"));
                  } else {
                    callback();
                  }
                }
              }
            },
            trigger: "blur",
          },
        ],
        password: [
          {
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请输入密码"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
          {
            min: 6,
            max: 32,
            message: "长度应为 6 到 32 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const tmp0 = this.ruleForm.password;
          const tmp1 = this.ruleForm.mobileNumber;
          this.ruleForm.password = this.$md5(tmp0 + tmp1);
          this.$axios
              .post(this.$serverUrl + "/user/signIn", this.ruleForm)
              .then((response) => {
                const user = response.data;
                if (user === "") {
                  this.$message({
                    showClose: true,
                    message: "账号或密码错误!",
                    type: "error",
                  });
                  return false;
                } else {
                  console.log(user.name);
                  this.$store.commit("setUser", user);
                  this.$router.push("/");
                  this.$message({
                    showClose: true,
                    message: "登录成功!",
                    type: "success",
                  });
                }
              })
              .catch((error) => {
                console.log(error);
                this.$message({
                  showClose: true,
                  message: "登录失败!",
                  type: "error",
                });
              });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.from {
  max-width: 500px;
  margin: 50px auto;
}
</style>